/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */


package com.environment_pro.monitor.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.environment_pro.monitor.model.TbRoom;
import com.environment_pro.monitor.service.TbRoomService;
import com.environment_pro.monitor.util.StaticUtil;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageHelper.Page;

@Controller
@RequestMapping("/tbRoom")
public class TbRoomController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(TbRoomController.class);

	@Autowired
	private TbRoomService tbRoomService;
	
	/**
	 * 获取所有列表 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbRoom")
	public String getAllTbRoom(TbRoom tbRoom, HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		//查询
		Page<TbRoom> mypage = tbRoomService.findAll(pageInfo,tbRoom);
		//返回查询条件和结果
		request.setAttribute("mypage", mypage);
		request.setAttribute("tbRoom", tbRoom);
		request.setAttribute("tbRoomList", mypage.getResult());
		//返回页面
		return "/TbRoom/allTbRoom";
	}
	
	/**
	 * 获取所有列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbRoomJson")
	@ResponseBody
	public List<TbRoom> getAllTbRoomJson(TbRoom tbRoom,HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		Page<TbRoom> mypage = tbRoomService.findAll(pageInfo,tbRoom);
		
		return mypage.getResult();
	}
	
	/**
	 * 跳转到添加界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddTbRoom")
	public String toAddTbRoom(HttpServletRequest request){
		
		return "/TbRoom/addTbRoom";
	}
	/**
	 * 添加并重定向
	 * @param tbRoom
	 * @param request
	 * @return
	 */
	@RequestMapping("/addTbRoom")
	public String addTbRoom(TbRoom tbRoom,HttpServletRequest request){
		tbRoomService.save(tbRoom);
		this.saveLogOperate(request,"");
		logger.info("");
		return "redirect:/tbRoom/getAllTbRoom";
	}
	
	/**
	 *编辑
	 * @param tbRoom
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateTbRoom")
	public String updateTbRoom(TbRoom tbRoom,HttpServletRequest request){
		
		if(tbRoomService.update(tbRoom)){
			//tbRoom = tbRoomService.findById(tbRoom.getId());
			request.setAttribute("tbRoom", tbRoom);
			this.saveLogOperate(request,"");
			return "redirect:/tbRoom/getAllTbRoom";
		}else{
			return "/error";
		}
	}
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbRoom")
	public String getTbRoom(int id,HttpServletRequest request){
		
		request.setAttribute("tbRoom", tbRoomService.findById(id));
		return "/TbRoom/editTbRoom";
	}
	
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbRoomJson")
	@ResponseBody
	public TbRoom getTbRoomJson(int id,HttpServletRequest request){
		return tbRoomService.findById(id);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delTbRoom")
	public void delTbRoom(int id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		TbRoom tbRoom = tbRoomService.findById(id);
		if(tbRoomService.delete(id)){
			result = "{\"result\":\"success\"}";
		}
		this.saveLogOperate(request,tbRoom.toString());
		response.setContentType("application/json");
		this.saveLogOperate(request,"");
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

